#include <boost/thread.hpp>
#include <iostream>
#include <time.h>
#include <windows.h>
#include <fstream>
#include <process.h>
#include <psapi.h>
#include <tchar.h>
#pragma comment(lib, "psapi.lib") 

float duration;
clock_t start, finish;
std::ofstream fout ("result.txt");

STARTUPINFO si;
PROCESS_INFORMATION pi;
PPROCESS_MEMORY_COUNTERS pmc = new PROCESS_MEMORY_COUNTERS;

void memFunc()
{
	DWORD exitCode;
	while(true)
	{
		GetExitCodeProcess(pi.hProcess, &exitCode);
		if (exitCode != STILL_ACTIVE)
		{
			GetProcessMemoryInfo(pi.hProcess, pmc, sizeof(PROCESS_MEMORY_COUNTERS));
			finish = clock();
			duration = (float)(finish - start) / CLOCKS_PER_SEC;
			fout << duration << std::endl << pmc->PeakPagefileUsage / 1024 << std::endl;
			exit(0);
		}
		Sleep(1);
	}
}
int main(int argc, char* argv[])
{
	ZeroMemory( &si, sizeof(si) );
	si.cb = sizeof(si);
	ZeroMemory( &pi, sizeof(pi) );
	
	TCHAR commandLine[MAX_PATH] = _T("task.exe");
	start = clock();
	if( !CreateProcess( NULL, commandLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
		exit(1);

	boost::thread memoryThread(memFunc);
	boost::this_thread::sleep(boost::posix_time::milliseconds(atoi(argv[1])));
	//TerminateProcess(pi.hProcess, exitCode);
	system("taskkill /f /im task.exe /t");
	
	memoryThread.join();
	CloseHandle( pi.hProcess );
	CloseHandle( pi.hThread );
	return 0;
}